#include "mainwidget.h"
#include "ui_mainwidget.h"

MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    resize(640,480);
    btn = new QPushButton("close",this);
    label = new QLabel("tittle",this);
    textEdit = new QTextEdit(this);
    QVBoxLayout *vbox  = new QVBoxLayout();
    vbox->addWidget(label);
    vbox->addWidget(textEdit);
    vbox->addWidget(btn);
    setLayout(vbox);
    // btn 所有事件被过滤
    btn->installEventFilter(this);
    textEdit->installEventFilter(this);
    connect(btn,&QPushButton::pressed,this,&MainWidget::close);
}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::mousePressEvent(QMouseEvent *event)
{
//    qDebug()<<"按下";
}

void MainWidget::mouseReleaseEvent(QMouseEvent *event)
{
//    qDebug()<<"释放";
}

bool MainWidget::event(QEvent *event)
{
    QWidget::event(event);
}
//                              控价类型            事件
bool MainWidget::eventFilter(QObject *watched, QEvent *event)
{
    //自定义过滤规则
    if(watched == btn)
    {
        if(event->type() == QEvent::MouseButtonPress)
        {
             qDebug()<<"按钮按下过滤"<<event;
             QMouseEvent* mouseEvent = dynamic_cast<QMouseEvent*>(event);
              if(mouseEvent->button() == Qt::LeftButton)
              {
                   qDebug()<<"左键按下过滤";
              }
              if(mouseEvent->button() == Qt::RightButton)
              {
                   qDebug()<<"右键按下过滤";
              }
             return true;
        }

    }
    if(watched == textEdit)
    {
        if(event->type()==QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
            textEdit->append(QString::number(keyEvent->key()));
            return true;
        }
    }
    //父类过滤
    return QWidget::eventFilter(watched,event);
}
